home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 10 / AACD 10.iso / AACD / Games / WarpQuake / Src / r_main.c < prev    next >
C/C++ Source or Header  |  2000-05-22  |  25KB  |  1,086 lines

  1. /*
  2. Copyright (C) 1996-1997 Id Software, Inc.
  3.  
  4. This program is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU General Public License
  6. as published by the Free Software Foundation; either version 2
  7. of the License, or (at your option) any later version.
  8.  
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  
  12.  
  13. See the GNU General Public License for more details.
  14.  
  15. You should have received a copy of the GNU General Public License
  16. along with this program; if not, write to the Free Software
  17. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
  18.  
  19. */
  20. // r_main.c
  21.  
  22. #include "quakedef.h"
  23. #include "r_local.h"
  24.  
  25. //define    PASSAGES
  26.  
  27. void        *colormap;
  28. float        r_time1;
  29. int            r_numallocatededges;
  30. qboolean    r_drawpolys;
  31. qboolean    r_drawculledpolys;
  32. qboolean    r_worldpolysbacktofront;
  33. qboolean    r_recursiveaffinetriangles = true;
  34. int            r_pixbytes = 1;
  35. float        r_aliasuvscale = 1.0;
  36. int            r_outofsurfaces;
  37. int            r_outofedges;
  38.  
  39. qboolean    r_dowarp, r_dowarpold, r_viewchanged;
  40.  
  41. int            numbtofpolys;
  42. btofpoly_t    *pbtofpolys;
  43. mvertex_t    *r_pcurrentvertbase;
  44.  
  45. int            c_surf;
  46. int            r_maxsurfsseen, r_maxedgesseen, r_cnumsurfs;
  47. static qboolean    r_surfsonstack;
  48. int            r_clipflags;
  49.  
  50. byte        *r_warpbuffer;
  51.  
  52. static byte        *r_stack_start;
  53.  
  54. static qboolean    r_fov_greater_than_90;
  55.  
  56. //
  57. // view origin
  58. //
  59. vec3_t    vup, base_vup;
  60. vec3_t    vpn, base_vpn;
  61. vec3_t    vright, base_vright;
  62. vec3_t    r_origin;
  63.  
  64. //
  65. // screen size info
  66. //
  67. refdef_t    r_refdef;
  68. float        xcenter, ycenter;
  69. float        xscale, yscale;
  70. float        xscaleinv, yscaleinv;
  71. float        xscaleshrink, yscaleshrink;
  72. float        aliasxscale, aliasyscale, aliasxcenter, aliasycenter;
  73.  
  74. int        screenwidth;
  75.  
  76. float    pixelAspect;
  77. float    screenAspect;
  78.  
  79. mplane_t    screenedge[4];
  80.  
  81. //
  82. // refresh flags
  83. //
  84. int        r_framecount = 1;    // so frame counts initialized to 0 don't match
  85. int        r_visframecount;
  86. int        d_spanpixcount;
  87. int        r_polycount;
  88. int        r_drawnpolycount;
  89. int        r_wholepolycount;
  90.  
  91. #define        VIEWMODNAME_LENGTH    256
  92. char        viewmodname[VIEWMODNAME_LENGTH+1];
  93. int            modcount;
  94.  
  95. int            *pfrustum_indexes[4];
  96. int            r_frustum_indexes[4*6];
  97.  
  98. int        reinit_surfcache = 1;    // if 1, surface cache is currently empty and
  99.                                 // must be reinitialized for current cache size
  100.  
  101. mleaf_t        *r_viewleaf, *r_oldviewleaf;
  102.  
  103. texture_t    *r_notexture_mip;
  104.  
  105. float        r_aliastransition, r_resfudge;
  106.  
  107. int        d_lightstylevalue[256];    // 8.8 fraction of base light value
  108.  
  109. float    dp_time1, dp_time2, db_time1, db_time2, rw_time1, rw_time2;
  110. float    se_time1, se_time2, de_time1, de_time2, dv_time1, dv_time2;
  111.  
  112. void R_MarkLeaves (void);
  113.  
  114. cvar_t    r_draworder = {"r_draworder","0"};
  115. cvar_t    r_speeds = {"r_speeds","0"};
  116. cvar_t    r_timegraph = {"r_timegraph","0"};
  117. cvar_t    r_graphheight = {"r_graphheight","10"};
  118. cvar_t    r_clearcolor = {"r_clearcolor","2"};
  119. cvar_t    r_waterwarp = {"r_waterwarp","1"};
  120. cvar_t    r_fullbright = {"r_fullbright","0"};
  121. cvar_t    r_drawentities = {"r_drawentities","1"};
  122. cvar_t    r_drawviewmodel = {"r_drawviewmodel","1"};
  123. cvar_t    r_aliasstats = {"r_polymodelstats","0"};
  124. cvar_t    r_dspeeds = {"r_dspeeds","0"};
  125. cvar_t    r_drawflat = {"r_drawflat", "0"};
  126. cvar_t    r_ambient = {"r_ambient", "0"};
  127. cvar_t    r_reportsurfout = {"r_reportsurfout", "0"};
  128. cvar_t    r_maxsurfs = {"r_maxsurfs", "0"};
  129. cvar_t    r_numsurfs = {"r_numsurfs", "0"};
  130. cvar_t    r_reportedgeout = {"r_reportedgeout", "0"};
  131. cvar_t    r_maxedges = {"r_maxedges", "0"};
  132. cvar_t    r_numedges = {"r_numedges", "0"};
  133. cvar_t    r_aliastransbase = {"r_aliastransbase", "200"};
  134. cvar_t    r_aliastransadj = {"r_aliastransadj", "100"};
  135.  
  136. extern cvar_t    scr_fov;
  137.  
  138. void CreatePassages (void);
  139. void SetVisibilityByPassages (void);
  140.  
  141. /*
  142. ==================
  143. R_InitTextures
  144. ==================
  145. */
  146. void    R_InitTextures (void)
  147. {
  148.     int        x,y, m;
  149.     byte    *dest;
  150.     
  151. // create a simple checkerboard texture for the default
  152.     r_notexture_mip = Hunk_AllocName (sizeof(texture_t) + 16*16+8*8+4*4+2*2, "notexture");
  153.     
  154.     r_notexture_mip->width = r_notexture_mip->height = 16;
  155.     r_notexture_mip->offsets[0] = sizeof(texture_t);
  156.     r_notexture_mip->offsets[1] = r_notexture_mip->offsets[0] + 16*16;
  157.     r_notexture_mip->offsets[2] = r_notexture_mip->offsets[1] + 8*8;
  158.     r_notexture_mip->offsets[3] = r_notexture_mip->offsets[2] + 4*4;
  159.     
  160.     for (m=0 ; m<4 ; m++)
  161.     {
  162.         dest = (byte *)r_notexture_mip + r_notexture_mip->offsets[m];
  163.         for (y=0 ; y< (16>>m) ; y++)
  164.             for (x=0 ; x< (16>>m) ; x++)
  165.             {
  166.                 if (  (y< (8>>m) ) ^ (x< (8>>m) ) )
  167.                     *dest++ = 0;
  168.                 else
  169.                     *dest++ = 0xff;
  170.             }
  171.     }    
  172. }
  173.  
  174. /*
  175. ===============
  176. R_Init
  177. ===============
  178. */
  179. void R_Init (void)
  180. {
  181.     int        dummy;
  182.     
  183. // get stack position so we can guess if we are going to overflow
  184.     r_stack_start = (byte *)&dummy;
  185.     
  186.     R_InitTurb ();
  187.     
  188.     Cmd_AddCommand ("timerefresh", R_TimeRefresh_f);    
  189.     Cmd_AddCommand ("pointfile", R_ReadPointFile_f);    
  190.  
  191.     Cvar_RegisterVariable (&r_draworder);
  192.     Cvar_RegisterVariable (&r_speeds);
  193.     Cvar_RegisterVariable (&r_timegraph);
  194.     Cvar_RegisterVariable (&r_graphheight);
  195.     Cvar_RegisterVariable (&r_drawflat);
  196.     Cvar_RegisterVariable (&r_ambient);
  197.     Cvar_RegisterVariable (&r_clearcolor);
  198.     Cvar_RegisterVariable (&r_waterwarp);
  199.     Cvar_RegisterVariable (&r_fullbright);
  200.     Cvar_RegisterVariable (&r_drawentities);
  201.     Cvar_RegisterVariable (&r_drawviewmodel);
  202.     Cvar_RegisterVariable (&r_aliasstats);
  203.     Cvar_RegisterVariable (&r_dspeeds);
  204.     Cvar_RegisterVariable (&r_reportsurfout);
  205.     Cvar_RegisterVariable (&r_maxsurfs);
  206.     Cvar_RegisterVariable (&r_numsurfs);
  207.     Cvar_RegisterVariable (&r_reportedgeout);
  208.     Cvar_RegisterVariable (&r_maxedges);
  209.     Cvar_RegisterVariable (&r_numedges);
  210.     Cvar_RegisterVariable (&r_aliastransbase);
  211.     Cvar_RegisterVariable (&r_aliastransadj);
  212.  
  213.     Cvar_SetValue ("r_maxedges", (float)NUMSTACKEDGES);
  214.     Cvar_SetValue ("r_maxsurfs", (float)NUMSTACKSURFACES);
  215.  
  216.     view_clipplanes[0].leftedge = true;
  217.     view_clipplanes[1].rightedge = true;
  218.     view_clipplanes[1].leftedge = view_clipplanes[2].leftedge =
  219.             view_clipplanes[3].leftedge = false;
  220.     view_clipplanes[0].rightedge = view_clipplanes[2].rightedge =
  221.             view_clipplanes[3].rightedge = false;
  222.  
  223.     r_refdef.xOrigin = XCENTERING;
  224.     r_refdef.yOrigin = YCENTERING;
  225.  
  226.     R_InitParticles ();
  227.  
  228. // TODO: collect 386-specific code in one place
  229. #if    id386
  230.     Sys_MakeCodeWriteable ((long)R_EdgeCodeStart,
  231.                          (long)R_EdgeCodeEnd - (long)R_EdgeCodeStart);
  232. #endif    // id386
  233.  
  234.     D_Init ();
  235. }
  236.  
  237. /*
  238. ===============
  239. R_NewMap
  240. ===============
  241. */
  242. void R_NewMap (void)
  243. {
  244.     int        i;
  245.     
  246. // clear out efrags in case the level hasn't been reloaded
  247. // FIXME: is this one short?
  248.     for (i=0 ; i<cl.worldmodel->numleafs ; i++)
  249.         cl.worldmodel->leafs[i].efrags = NULL;
  250.              
  251.     r_viewleaf = NULL;
  252.     R_ClearParticles ();
  253.  
  254.     r_cnumsurfs = r_maxsurfs.value;
  255.  
  256.     if (r_cnumsurfs <= MINSURFACES)
  257.         r_cnumsurfs = MINSURFACES;
  258.  
  259.     if (r_cnumsurfs > NUMSTACKSURFACES)
  260.     {
  261.         surfaces = Hunk_AllocName (r_cnumsurfs * sizeof(surf_t), "surfaces");
  262.         surface_p = surfaces;
  263.         surf_max = &surfaces[r_cnumsurfs];
  264.         r_surfsonstack = false;
  265.     // surface 0 doesn't really exist; it's just a dummy because index 0
  266.     // is used to indicate no edge attached to surface
  267.         surfaces--;
  268.         R_SurfacePatch ();
  269.     }
  270.     else
  271.     {
  272.         r_surfsonstack = true;
  273.     }
  274.  
  275.     r_maxedgesseen = 0;
  276.     r_maxsurfsseen = 0;
  277.  
  278.     r_numallocatededges = r_maxedges.value;
  279.  
  280.     if (r_numallocatededges < MINEDGES)
  281.         r_numallocatededges = MINEDGES;
  282.  
  283.     if (r_numallocatededges <= NUMSTACKEDGES)
  284.     {
  285.         auxedges = NULL;
  286.     }
  287.     else
  288.     {
  289.         auxedges = Hunk_AllocName (r_numallocatededges * sizeof(edge_t),
  290.                                    "edges");
  291.     }
  292.  
  293.     r_dowarpold = false;
  294.     r_viewchanged = false;
  295. #ifdef PASSAGES
  296. CreatePassages ();
  297. #endif
  298. }
  299.  
  300.  
  301. /*
  302. ===============
  303. R_SetVrect
  304. ===============
  305. */
  306. void R_SetVrect (vrect_t *pvrectin, vrect_t *pvrect, int lineadj)
  307. {
  308.     int        h;
  309.     float    size;
  310.  
  311.     size = scr_viewsize.value > 100 ? 100 : scr_viewsize.value;
  312.     if (cl.intermission)
  313.     {
  314.         size = 100;
  315.         lineadj = 0;
  316.     }
  317.     size /= 100;
  318.  
  319.     h = pvrectin->height - lineadj;
  320.     pvrect->width = pvrectin->width * size;
  321.     if (pvrect->width < 96)
  322.     {
  323.         size = 96.0 / pvrectin->width;
  324.         pvrect->width = 96;    // min for icons
  325.     }
  326.     pvrect->width &= ~7;
  327.     pvrect->height = pvrectin->height * size;
  328.     if (pvrect->height > pvrectin->height - lineadj)
  329.         pvrect->height = pvrectin->height - lineadj;
  330.  
  331.     pvrect->height &= ~1;
  332.  
  333.     pvrect->x = (pvrectin->width - pvrect->width)/2;
  334.     pvrect->y = (h - pvrect->height)/2;
  335.  
  336.     {
  337.         if (lcd_x.value)
  338.         {
  339.             pvrect->y >>= 1;
  340.             pvrect->height >>= 1;
  341.         }
  342.     }
  343. }
  344.  
  345.  
  346. /*
  347. ===============
  348. R_ViewChanged
  349.  
  350. Called every time the vid structure or r_refdef changes.
  351. Guaranteed to be called before the first refresh
  352. ===============
  353. */
  354. void R_ViewChanged (vrect_t *pvrect, int lineadj, float aspect)
  355. {
  356.     int        i;
  357.     float    res_scale;
  358.     float verticalFieldOfView;
  359.     float    xOrigin, yOrigin;
  360.  
  361.     r_viewchanged = true;
  362.  
  363.     R_SetVrect (pvrect, &r_refdef.vrect, lineadj);
  364.  
  365.     r_refdef.horizontalFieldOfView = 2.0 * tan (r_refdef.fov_x/360*M_PI);
  366.     r_refdef.fvrectx = (float)r_refdef.vrect.x;
  367.     r_refdef.fvrectx_adj = (float)r_refdef.vrect.x - 0.5;
  368.     r_refdef.vrect_x_adj_shift20 = (r_refdef.vrect.x<<20) + (1<<19) - 1;
  369.     r_refdef.fvrecty = (float)r_refdef.vrect.y;
  370.     r_refdef.fvrecty_adj = (float)r_refdef.vrect.y - 0.5;
  371.     r_refdef.vrectright = r_refdef.vrect.x + r_refdef.vrect.width;
  372.     r_refdef.vrectright_adj_shift20 = (r_refdef.vrectright<<20) + (1<<19) - 1;
  373.     r_refdef.fvrectright = (float)r_refdef.vrectright;
  374.     r_refdef.fvrectright_adj = (float)r_refdef.vrectright - 0.5;
  375.     r_refdef.vrectrightedge = (float)r_refdef.vrectright - 0.99;
  376.     r_refdef.vrectbottom = r_refdef.vrect.y + r_refdef.vrect.height;
  377.     r_refdef.fvrectbottom = (float)r_refdef.vrectbottom;
  378.     r_refdef.fvrectbottom_adj = (float)r_refdef.vrectbottom - 0.5;
  379.  
  380.     r_refdef.aliasvrect.x = (int)(r_refdef.vrect.x * r_aliasuvscale);
  381.     r_refdef.aliasvrect.y = (int)(r_refdef.vrect.y * r_aliasuvscale);
  382.     r_refdef.aliasvrect.width = (int)(r_refdef.vrect.width * r_aliasuvscale);
  383.     r_refdef.aliasvrect.height = (int)(r_refdef.vrect.height * r_aliasuvscale);
  384.     r_refdef.aliasvrectright = r_refdef.aliasvrect.x +
  385.             r_refdef.aliasvrect.width;
  386.     r_refdef.aliasvrectbottom = r_refdef.aliasvrect.y +
  387.             r_refdef.aliasvrect.height;
  388.  
  389.     pixelAspect = aspect;
  390.     xOrigin = r_refdef.xOrigin;
  391.     yOrigin = r_refdef.yOrigin;
  392.     
  393.     screenAspect = r_refdef.vrect.width*pixelAspect /
  394.             r_refdef.vrect.height;
  395. // 320*200 1.0 pixelAspect = 1.6 screenAspect
  396. // 320*240 1.0 pixelAspect = 1.3333 screenAspect
  397. // proper 320*200 pixelAspect = 0.8333333
  398.  
  399.     verticalFieldOfView = r_refdef.horizontalFieldOfView / screenAspect;
  400.  
  401. // values for perspective projection
  402. // if math were exact, the values would range from 0.5 to to range+0.5
  403. // hopefully they wll be in the 0.000001 to range+.999999 and truncate
  404. // the polygon rasterization will never render in the first row or column
  405. // but will definately render in the [range] row and column, so adjust the
  406. // buffer origin to get an exact edge to edge fill
  407.     xcenter = ((float)r_refdef.vrect.width * XCENTERING) +
  408.             r_refdef.vrect.x - 0.5;
  409.     aliasxcenter = xcenter * r_aliasuvscale;
  410.     ycenter = ((float)r_refdef.vrect.height * YCENTERING) +
  411.             r_refdef.vrect.y - 0.5;
  412.     aliasycenter = ycenter * r_aliasuvscale;
  413.  
  414.     xscale = r_refdef.vrect.width / r_refdef.horizontalFieldOfView;
  415.     aliasxscale = xscale * r_aliasuvscale;
  416.     xscaleinv = 1.0 / xscale;
  417.     yscale = xscale * pixelAspect;
  418.     aliasyscale = yscale * r_aliasuvscale;
  419.     yscaleinv = 1.0 / yscale;
  420.     xscaleshrink = (r_refdef.vrect.width-6)/r_refdef.horizontalFieldOfView;
  421.     yscaleshrink = xscaleshrink*pixelAspect;
  422.  
  423. // left side clip
  424.     screenedge[0].normal[0] = -1.0 / (xOrigin*r_refdef.horizontalFieldOfView);
  425.     screenedge[0].normal[1] = 0;
  426.     screenedge[0].normal[2] = 1;
  427.     screenedge[0].type = PLANE_ANYZ;
  428.     
  429. // right side clip
  430.     screenedge[1].normal[0] =
  431.             1.0 / ((1.0-xOrigin)*r_refdef.horizontalFieldOfView);
  432.     screenedge[1].normal[1] = 0;
  433.     screenedge[1].normal[2] = 1;
  434.     screenedge[1].type = PLANE_ANYZ;
  435.     
  436. // top side clip
  437.     screenedge[2].normal[0] = 0;
  438.     screenedge[2].normal[1] = -1.0 / (yOrigin*verticalFieldOfView);
  439.     screenedge[2].normal[2] = 1;
  440.     screenedge[2].type = PLANE_ANYZ;
  441.     
  442. // bottom side clip
  443.     screenedge[3].normal[0] = 0;
  444.     screenedge[3].normal[1] = 1.0 / ((1.0-yOrigin)*verticalFieldOfView);
  445.     screenedge[3].normal[2] = 1;    
  446.     screenedge[3].type = PLANE_ANYZ;
  447.     
  448.     for (i=0 ; i<4 ; i++)
  449.         VectorNormalize (screenedge[i].normal);
  450.  
  451.     res_scale = sqrt ((double)(r_refdef.vrect.width * r_refdef.vrect.height) /
  452.                       (320.0 * 152.0)) *
  453.             (2.0 / r_refdef.horizontalFieldOfView);
  454.     r_aliastransition = r_aliastransbase.value * res_scale;
  455.     r_resfudge = r_aliastransadj.value * res_scale;
  456.  
  457.     if (scr_fov.value <= 90.0)
  458.         r_fov_greater_than_90 = false;
  459.     else
  460.         r_fov_greater_than_90 = true;
  461.  
  462. // TODO: collect 386-specific code in one place
  463. #if    id386
  464.     if (r_pixbytes == 1)
  465.     {
  466.         Sys_MakeCodeWriteable ((long)R_Surf8Start,
  467.                              (long)R_Surf8End - (long)R_Surf8Start);
  468.         colormap = vid.colormap;
  469.         R_Surf8Patch ();
  470.     }
  471.     else
  472.     {
  473.         Sys_MakeCodeWriteable ((long)R_Surf16Start,
  474.                              (long)R_Surf16End - (long)R_Surf16Start);
  475.         colormap = vid.colormap16;
  476.         R_Surf16Patch ();
  477.     }
  478. #endif    // id386
  479.  
  480.     D_ViewChanged ();
  481. }
  482.  
  483.  
  484. /*
  485. ===============
  486. R_MarkLeaves
  487. ===============
  488. */
  489. void R_MarkLeaves (void)
  490. {
  491.     byte    *vis;
  492.     mnode_t    *node;
  493.     int        i;
  494.  
  495.     if (r_oldviewleaf == r_viewleaf)
  496.         return;
  497.     
  498.     r_visframecount++;
  499.     r_oldviewleaf = r_viewleaf;
  500.  
  501.     vis = Mod_LeafPVS (r_viewleaf, cl.worldmodel);
  502.         
  503.     for (i=0 ; i<cl.worldmodel->numleafs ; i++)
  504.     {
  505.         if (vis[i>>3] & (1<<(i&7)))
  506.         {
  507.             node = (mnode_t *)&cl.worldmodel->leafs[i+1];
  508.             do
  509.             {
  510.                 if (node->visframe == r_visframecount)
  511.                     break;
  512.                 node->visframe = r_visframecount;
  513.                 node = node->parent;
  514.             } while (node);
  515.         }
  516.     }
  517. }
  518.  
  519.  
  520. /*
  521. =============
  522. R_DrawEntitiesOnList
  523. =============
  524. */
  525. void R_DrawEntitiesOnList (void)
  526. {
  527.     int            i, j;
  528.     int            lnum;
  529.     alight_t    lighting;
  530. // FIXME: remove and do real lighting
  531.     float        lightvec[3] = {-1, 0, 0};
  532.     vec3_t        dist;
  533.     float        add;
  534.  
  535.     if (!r_drawentities.value)
  536.         return;
  537.  
  538.     for (i=0 ; i<cl_numvisedicts ; i++)
  539.     {
  540.         currententity = cl_visedicts[i];
  541.  
  542.         if (currententity == &cl_entities[cl.viewentity])
  543.             continue;    // don't draw the player
  544.  
  545.         switch (currententity->model->type)
  546.         {
  547.         case mod_sprite:
  548.             VectorCopy (currententity->origin, r_entorigin);
  549.             VectorSubtract (r_origin, r_entorigin, modelorg);
  550.             R_DrawSprite ();
  551.             break;
  552.  
  553.         case mod_alias:
  554.             VectorCopy (currententity->origin, r_entorigin);
  555.             VectorSubtract (r_origin, r_entorigin, modelorg);
  556.  
  557.         // see if the bounding box lets us trivially reject, also sets
  558.         // trivial accept status
  559.             if (R_AliasCheckBBox ())
  560.             {
  561.                 j = R_LightPoint (currententity->origin);
  562.     
  563.                 lighting.ambientlight = j;
  564.                 lighting.shadelight = j;
  565.  
  566.                 lighting.plightvec = lightvec;
  567.  
  568.                 for (lnum=0 ; lnum<MAX_DLIGHTS ; lnum++)
  569.                 {
  570.                     if (cl_dlights[lnum].die >= cl.time)
  571.                     {
  572.                         VectorSubtract (currententity->origin,
  573.                                         cl_dlights[lnum].origin,
  574.                                         dist);
  575.                         add = cl_dlights[lnum].radius - Length(dist);
  576.     
  577.                         if (add > 0)
  578.                             lighting.ambientlight += add;
  579.                     }
  580.                 }
  581.     
  582.             // clamp lighting so it doesn't overbright as much
  583.                 if (lighting.ambientlight > 128)
  584.                     lighting.ambientlight = 128;
  585.                 if (lighting.ambientlight + lighting.shadelight > 192)
  586.                     lighting.shadelight = 192 - lighting.ambientlight;
  587.  
  588.                 R_AliasDrawModel (&lighting);
  589.             }
  590.  
  591.             break;
  592.  
  593.         default:
  594.             break;
  595.         }
  596.     }
  597. }
  598.  
  599. /*
  600. =============
  601. R_DrawViewModel
  602. =============
  603. */
  604. void R_DrawViewModel (void)
  605. {
  606. // FIXME: remove and do real lighting
  607.     float        lightvec[3] = {-1, 0, 0};
  608.     int            j;
  609.     int            lnum;
  610.     vec3_t        dist;
  611.     float        add;
  612.     dlight_t    *dl;
  613.     vec3_t        viewlightvec;
  614.     alight_t    r_viewlighting = {128, 192, viewlightvec};
  615.  
  616.     if (!r_drawviewmodel.value || r_fov_greater_than_90)
  617.         return;
  618.  
  619.     if (cl.items & IT_INVISIBILITY)
  620.         return;
  621.  
  622.     if (cl.stats[STAT_HEALTH] <= 0)
  623.         return;
  624.  
  625.     currententity = &cl.viewent;
  626.     if (!currententity->model)
  627.         return;
  628.  
  629.     VectorCopy (currententity->origin, r_entorigin);
  630.     VectorSubtract (r_origin, r_entorigin, modelorg);
  631.  
  632.     VectorCopy (vup, viewlightvec);
  633.     VectorInverse (viewlightvec);
  634.  
  635.     j = R_LightPoint (currententity->origin);
  636.  
  637.     if (j < 24)
  638.         j = 24;        // allways give some light on gun
  639.     r_viewlighting.ambientlight = j;
  640.     r_viewlighting.shadelight = j;
  641.  
  642. // add dynamic lights        
  643.     for (lnum=0 ; lnum<MAX_DLIGHTS ; lnum++)
  644.     {
  645.         dl = &cl_dlights[lnum];
  646.         if (!dl->radius)
  647.             continue;
  648.         if (!dl->radius)
  649.             continue;
  650.         if (dl->die < cl.time)
  651.             continue;
  652.  
  653.         VectorSubtract (currententity->origin, dl->origin, dist);
  654.         add = dl->radius - Length(dist);
  655.         if (add > 0)
  656.             r_viewlighting.ambientlight += add;
  657.     }
  658.  
  659. // clamp lighting so it doesn't overbright as much
  660.     if (r_viewlighting.ambientlight > 128)
  661.         r_viewlighting.ambientlight = 128;
  662.     if (r_viewlighting.ambientlight + r_viewlighting.shadelight > 192)
  663.         r_viewlighting.shadelight = 192 - r_viewlighting.ambientlight;
  664.  
  665.     r_viewlighting.plightvec = lightvec;
  666.  
  667. #ifdef QUAKE2
  668.     cl.light_level = r_viewlighting.ambientlight;
  669. #endif
  670.  
  671.     R_AliasDrawModel (&r_viewlighting);
  672. }
  673.  
  674.  
  675. /*
  676. =============
  677. R_BmodelCheckBBox
  678. =============
  679. */
  680. int R_BmodelCheckBBox (model_t *clmodel, float *minmaxs)
  681. {
  682.     int            i, *pindex, clipflags;
  683.     vec3_t        acceptpt, rejectpt;
  684.     double        d;
  685.  
  686.     clipflags = 0;
  687.  
  688.     if (currententity->angles[0] || currententity->angles[1]
  689.         || currententity->angles[2])
  690.     {
  691.         for (i=0 ; i<4 ; i++)
  692.         {
  693.             d = DotProduct (currententity->origin, view_clipplanes[i].normal);
  694.             d -= view_clipplanes[i].dist;
  695.  
  696.             if (d <= -clmodel->radius)
  697.                 return BMODEL_FULLY_CLIPPED;
  698.  
  699.             if (d <= clmodel->radius)
  700.                 clipflags |= (1<<i);
  701.         }
  702.     }
  703.     else
  704.     {
  705.         for (i=0 ; i<4 ; i++)
  706.         {
  707.         // generate accept and reject points
  708.         // FIXME: do with fast look-ups or integer tests based on the sign bit
  709.         // of the floating point values
  710.  
  711.             pindex = pfrustum_indexes[i];
  712.  
  713.             rejectpt[0] = minmaxs[pindex[0]];
  714.             rejectpt[1] = minmaxs[pindex[1]];
  715.             rejectpt[2] = minmaxs[pindex[2]];
  716.             
  717.             d = DotProduct (rejectpt, view_clipplanes[i].normal);
  718.             d -= view_clipplanes[i].dist;
  719.  
  720.             if (d <= 0)
  721.                 return BMODEL_FULLY_CLIPPED;
  722.  
  723.             acceptpt[0] = minmaxs[pindex[3+0]];
  724.             acceptpt[1] = minmaxs[pindex[3+1]];
  725.             acceptpt[2] = minmaxs[pindex[3+2]];
  726.  
  727.             d = DotProduct (acceptpt, view_clipplanes[i].normal);
  728.             d -= view_clipplanes[i].dist;
  729.  
  730.             if (d <= 0)
  731.                 clipflags |= (1<<i);
  732.         }
  733.     }
  734.  
  735.     return clipflags;
  736. }
  737.  
  738.  
  739. /*
  740. =============
  741. R_DrawBEntitiesOnList
  742. =============
  743. */
  744. void R_DrawBEntitiesOnList (void)
  745. {
  746.     int            i, j, k, clipflags;
  747.     vec3_t        oldorigin;
  748.     model_t        *clmodel;
  749.     float        minmaxs[6];
  750.  
  751.     if (!r_drawentities.value)
  752.         return;
  753.  
  754.     VectorCopy (modelorg, oldorigin);
  755.     insubmodel = true;
  756.     r_dlightframecount = r_framecount;
  757.  
  758.     for (i=0 ; i<cl_numvisedicts ; i++)
  759.     {
  760.         currententity = cl_visedicts[i];
  761.  
  762.         switch (currententity->model->type)
  763.         {
  764.         case mod_brush:
  765.  
  766.             clmodel = currententity->model;
  767.  
  768.         // see if the bounding box lets us trivially reject, also sets
  769.         // trivial accept status
  770.             for (j=0 ; j<3 ; j++)
  771.             {
  772.                 minmaxs[j] = currententity->origin[j] +
  773.                         clmodel->mins[j];
  774.                 minmaxs[3+j] = currententity->origin[j] +
  775.                         clmodel->maxs[j];
  776.             }
  777.  
  778.             clipflags = R_BmodelCheckBBox (clmodel, minmaxs);
  779.  
  780.             if (clipflags != BMODEL_FULLY_CLIPPED)
  781.             {
  782.                 VectorCopy (currententity->origin, r_entorigin);
  783.                 VectorSubtract (r_origin, r_entorigin, modelorg);
  784.             // FIXME: is this needed?
  785.                 VectorCopy (modelorg, r_worldmodelorg);
  786.         
  787.                 r_pcurrentvertbase = clmodel->vertexes;
  788.         
  789.             // FIXME: stop transforming twice
  790.                 R_RotateBmodel ();
  791.  
  792.             // calculate dynamic lighting for bmodel if it's not an
  793.             // instanced model
  794.                 if (clmodel->firstmodelsurface != 0)
  795.                 {
  796.                     for (k=0 ; k<MAX_DLIGHTS ; k++)
  797.                     {
  798.                         if ((cl_dlights[k].die < cl.time) ||
  799.                             (!cl_dlights[k].radius))
  800.                         {
  801.                             continue;
  802.                         }
  803.  
  804.                         R_MarkLights (&cl_dlights[k], 1<<k,
  805.                             clmodel->nodes + clmodel->hulls[0].firstclipnode);
  806.                     }
  807.                 }
  808.  
  809.             // if the driver wants polygons, deliver those. Z-buffering is on
  810.             // at this point, so no clipping to the world tree is needed, just
  811.             // frustum clipping
  812.                 if (r_drawpolys | r_drawculledpolys)
  813.                 {
  814.                     R_ZDrawSubmodelPolys (clmodel);
  815.                 }
  816.                 else
  817.                 {
  818.                     r_pefragtopnode = NULL;
  819.  
  820.                     for (j=0 ; j<3 ; j++)
  821.                     {
  822.                         r_emins[j] = minmaxs[j];
  823.                         r_emaxs[j] = minmaxs[3+j];
  824.                     }
  825.  
  826.                     R_SplitEntityOnNode2 (cl.worldmodel->nodes);
  827.  
  828.                     if (r_pefragtopnode)
  829.                     {
  830.                         currententity->topnode = r_pefragtopnode;
  831.     
  832.                         if (r_pefragtopnode->contents >= 0)
  833.                         {
  834.                         // not a leaf; has to be clipped to the world BSP
  835.                             r_clipflags = clipflags;
  836.                             R_DrawSolidClippedSubmodelPolygo (clmodel);
  837.                         }
  838.                         else
  839.                         {
  840.                         // falls entirely in one leaf, so we just put all the
  841.                         // edges in the edge list and let 1/z sorting handle
  842.                         // drawing order
  843.                             R_DrawSubmodelPolygons (clmodel, clipflags);
  844.                         }
  845.     
  846.                         currententity->topnode = NULL;
  847.                     }
  848.                 }
  849.  
  850.             // put back world rotation and frustum clipping        
  851.             // FIXME: R_RotateBmodel should just work off base_vxx
  852.                 VectorCopy (base_vpn, vpn);
  853.                 VectorCopy (base_vup, vup);
  854.                 VectorCopy (base_vright, vright);
  855.                 VectorCopy (base_modelorg, modelorg);
  856.                 VectorCopy (oldorigin, modelorg);
  857.                 R_TransformFrustum ();
  858.             }
  859.  
  860.             break;
  861.  
  862.         default:
  863.             break;
  864.         }
  865.     }
  866.  
  867.     insubmodel = false;
  868. }
  869.  
  870.  
  871. /*
  872. ================
  873. R_EdgeDrawing
  874. ================
  875. */
  876. void R_EdgeDrawing (void)
  877. {
  878.     edge_t    ledges[NUMSTACKEDGES +
  879.                 ((CACHE_SIZE - 1) / sizeof(edge_t)) + 1];
  880.     surf_t    lsurfs[NUMSTACKSURFACES +
  881.                 ((CACHE_SIZE - 1) / sizeof(surf_t)) + 1];
  882.  
  883.     if (auxedges)
  884.     {
  885.         r_edges = auxedges;
  886.     }
  887.     else
  888.     {
  889.         r_edges =  (edge_t *)
  890.                 (((long)&ledges[0] + CACHE_SIZE - 1) & ~(CACHE_SIZE - 1));
  891.     }
  892.  
  893.     if (r_surfsonstack)
  894.     {
  895.         surfaces =  (surf_t *)
  896.                 (((long)&lsurfs[0] + CACHE_SIZE - 1) & ~(CACHE_SIZE - 1));
  897.         surf_max = &surfaces[r_cnumsurfs];
  898.     // surface 0 doesn't really exist; it's just a dummy because index 0
  899.     // is used to indicate no edge attached to surface
  900.         surfaces--;
  901.         R_SurfacePatch ();
  902.     }
  903.  
  904.     R_BeginEdgeFrame ();
  905.  
  906.     if (r_dspeeds.value)
  907.     {
  908.         rw_time1 = Sys_FloatTime ();
  909.     }
  910.  
  911.     R_RenderWorld ();
  912.  
  913.     if (r_drawculledpolys)
  914.         R_ScanEdges ();
  915.  
  916. // only the world can be drawn back to front with no z reads or compares, just
  917. // z writes, so have the driver turn z compares on now
  918.     D_TurnZOn ();
  919.  
  920.     if (r_dspeeds.value)
  921.     {
  922.         rw_time2 = Sys_FloatTime ();
  923.         db_time1 = rw_time2;
  924.     }
  925.  
  926.     R_DrawBEntitiesOnList ();
  927.  
  928.     if (r_dspeeds.value)
  929.     {
  930.         db_time2 = Sys_FloatTime ();
  931.         se_time1 = db_time2;
  932.     }
  933.  
  934.     if (!r_dspeeds.value)
  935.     {
  936.         VID_UnlockBuffer ();
  937.         S_ExtraUpdate ();    // don't let sound get messed up if going slow
  938.         VID_LockBuffer ();
  939.     }
  940.     
  941.     if (!(r_drawpolys | r_drawculledpolys))
  942.         R_ScanEdges ();
  943. }
  944.  
  945.  
  946. /*
  947. ================
  948. R_RenderView
  949.  
  950. r_refdef must be set before the first call
  951. ================
  952. */
  953. void R_RenderView_ (void)
  954. {
  955.     byte    warpbuffer[WARP_WIDTH * WARP_HEIGHT];
  956.  
  957.     r_warpbuffer = warpbuffer;
  958.  
  959.     if (r_timegraph.value || r_speeds.value || r_dspeeds.value)
  960.         r_time1 = Sys_FloatTime ();
  961.  
  962.     R_SetupFrame ();
  963.  
  964. #ifdef PASSAGES
  965. SetVisibilityByPassages ();
  966. #else
  967.     R_MarkLeaves ();    // done here so we know if we're in water
  968. #endif
  969.  
  970. // make FDIV fast. This reduces timing precision after we've been running for a
  971. // while, so we don't do it globally.  This also sets chop mode, and we do it
  972. // here so that setup stuff like the refresh area calculations match what's
  973. // done in screen.c
  974.     Sys_LowFPPrecision ();
  975.  
  976.     if (!cl_entities[0].model || !cl.worldmodel)
  977.         Sys_Error ("R_RenderView: NULL worldmodel");
  978.         
  979.     if (!r_dspeeds.value)
  980.     {
  981.         VID_UnlockBuffer ();
  982.         S_ExtraUpdate ();    // don't let sound get messed up if going slow
  983.         VID_LockBuffer ();
  984.     }
  985.     
  986.     R_EdgeDrawing ();
  987.  
  988.     if (!r_dspeeds.value)
  989.     {
  990.         VID_UnlockBuffer ();
  991.         S_ExtraUpdate ();    // don't let sound get messed up if going slow
  992.         VID_LockBuffer ();
  993.     }
  994.     
  995.     if (r_dspeeds.value)
  996.     {
  997.         se_time2 = Sys_FloatTime ();
  998.         de_time1 = se_time2;
  999.     }
  1000.  
  1001.     R_DrawEntitiesOnList ();
  1002.  
  1003.     if (r_dspeeds.value)
  1004.     {
  1005.         de_time2 = Sys_FloatTime ();
  1006.         dv_time1 = de_time2;
  1007.     }
  1008.  
  1009.     R_DrawViewModel ();
  1010.  
  1011.     if (r_dspeeds.value)
  1012.     {
  1013.         dv_time2 = Sys_FloatTime ();
  1014.         dp_time1 = Sys_FloatTime ();
  1015.     }
  1016.  
  1017.     R_DrawParticles ();
  1018.  
  1019.     if (r_dspeeds.value)
  1020.         dp_time2 = Sys_FloatTime ();
  1021.  
  1022.     if (r_dowarp)
  1023.         D_WarpScreen ();
  1024.  
  1025.     V_SetContentsColor (r_viewleaf->contents);
  1026.  
  1027.     if (r_timegraph.value)
  1028.         R_TimeGraph ();
  1029.  
  1030.     if (r_aliasstats.value)
  1031.         R_PrintAliasStats ();
  1032.         
  1033.     if (r_speeds.value)
  1034.         R_PrintTimes ();
  1035.  
  1036.     if (r_dspeeds.value)
  1037.         R_PrintDSpeeds ();
  1038.  
  1039.     if (r_reportsurfout.value && r_outofsurfaces)
  1040.         Con_Printf ("Short %d surfaces\n", r_outofsurfaces);
  1041.  
  1042.     if (r_reportedgeout.value && r_outofedges)
  1043.         Con_Printf ("Short roughly %d edges\n", r_outofedges * 2 / 3);
  1044.  
  1045. // back to high floating-point precision
  1046.     Sys_HighFPPrecision ();
  1047. }
  1048.  
  1049. void R_RenderView (void)
  1050. {
  1051.     int        dummy;
  1052.     int        delta;
  1053.     
  1054.     delta = (byte *)&dummy - r_stack_start;
  1055.     if (delta < -10000 || delta > 10000)
  1056.         Sys_Error ("R_RenderView: called without enough stack");
  1057.  
  1058.     if ( Hunk_LowMark() & 3 )
  1059.         Sys_Error ("Hunk is missaligned");
  1060.  
  1061.     if ( (long)(&dummy) & 3 )
  1062.         Sys_Error ("Stack is missaligned");
  1063.  
  1064.     if ( (long)(&r_warpbuffer) & 3 )
  1065.         Sys_Error ("Globals are missaligned");
  1066.  
  1067.     R_RenderView_ ();
  1068. }
  1069.  
  1070. /*
  1071. ================
  1072. R_InitTurb
  1073. ================
  1074. */
  1075. void R_InitTurb (void)
  1076. {
  1077.     int        i;
  1078.     
  1079.     for (i=0 ; i<(SIN_BUFFER_SIZE) ; i++)
  1080.     {
  1081.         sintable[i] = AMP + sin(i*3.14159*2/CYCLE)*AMP;
  1082.         intsintable[i] = AMP2 + sin(i*3.14159*2/CYCLE)*AMP2;    // AMP2, not 20
  1083.     }
  1084. }
  1085.  
  1086.